<?php
/*
导出"已卖出和已买到产品"Excel
*/
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{
	// echo $pOrder->orderlistTotal($currentPage,$pageSize);
	// exit;
	// print_r();
	require_once 'class/PHPExcel.php'; 
	require_once('class/PHPExcel/Writer/Excel2007.php');  	
	$objExcel = new PHPExcel();
	$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;  
	$cacheSettings = array( ' memoryCacheSize '  => '8MB' );  
	$objExcel->setActiveSheetIndex(0);       
	$objActSheet = $objExcel->getActiveSheet();       	
	$objWriter = new PHPExcel_Writer_Excel5($objExcel);
	$n=1;
	$factor='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	for($j = 0; $j<13;$j++){
		$objActSheet->getColumnDimension($factor[$j])->setAutoSize(false); 
	}
	
	$objActSheet->getStyle('G')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
	$objActSheet->setCellValue('A'.$n, "订单编号");    
	$objActSheet->setCellValue('B'.$n, "下单时间");  
	$objActSheet->setCellValue('C'.$n, "游玩时间");
	$objActSheet->setCellValue('D'.$n, "完成时间");
	
	switch($_REQUEST['atype']){
		case 'buy':
			$filename=date('YmdHis').'买到产品订单列表';
			$objActSheet->setCellValue('E'.$n, "供应商");
			break;
		case 'sale':
			$filename=date('YmdHis').'卖出产品订单列表';
			$objActSheet->setCellValue('E'.$n, "分销商");
			break;
		default:
			break;
	}
	$objActSheet->setCellValue('F'.$n, "取票人");
	$objActSheet->setCellValue('G'.$n, "手机");
	$objActSheet->setCellValue('H'.$n, "类别");
	$objActSheet->setCellValue('I'.$n, "票类单号");
	$objActSheet->setCellValue('J'.$n, "产品名称");
	$objActSheet->setCellValue('K'.$n, "单价(元)");
	$objActSheet->setCellValue('L'.$n, "数量");
	$objActSheet->setCellValue('M'.$n, "支付方式");
	$objActSheet->setCellValue('N'.$n, "订单状态");
	$objActSheet->setCellValue('O'.$n, "总金额(元)");
	$objActSheet->setCellValue('P'.$n, "备注");
	$objActSheet->getStyle('A'.$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
	// print_r($r);
	// exit;
	$i=1;
	foreach($r as $lid=>$row){
		// print_r($row);
		// exit;
		$n++;
		$objActSheet->setCellValue('A'.$n,$row['mainOrder']->UUordernum);    
		$objActSheet->setCellValue('B'.$n,$row['mainOrder']->UUordertime);
		$objActSheet->setCellValue('C'.$n,$row['mainOrder']->UUbegintime);
		$objActSheet->setCellValue('D'.$n,$row['mainOrder']->UUdtime!="0000-00-00 00:00:00"?(string)$row['mainOrder']->UUdtime:"未验证");
		if($_REQUEST['atype']=='buy'){
			$objActSheet->setCellValue('E'.$n,$anames[(string)$row['mainOrder']->UUaid]);
		}else{
			$objActSheet->setCellValue('E'.$n,$fnames[(int)$row['mainOrder']->fid]?$fnames[(int)$row['mainOrder']->fid]:(string)$row['mainOrder']->UUdname);
		}
		$objActSheet->setCellValue('F'.$n,(string)$row['mainOrder']->UUordername);
		$objActSheet->setCellValue('G'.$n,(string)$row['mainOrder']->UUordertel);
		$objActSheet->setCellValue('H'.$n,OrderRecord::$p_typeT[(string)$row['mainOrder']->UUp_type]);
		$objActSheet->setCellValue('I'.$n,$row['mainOrder']->UUordernum);
		$objActSheet->setCellValue('J'.$n,(string)$row['mainOrder']->UUltitle.(string)$row['mainOrder']->UUttitle);
		$objActSheet->setCellValue('K'.$n,(string)$row['mainOrder']->UUtprice/100);
		$objActSheet->setCellValue('L'.$n,(string)$row['mainOrder']->UUtnum);
		$objActSheet->setCellValue('M'.$n,OrderRecord::$paymodeT[(string)$row['mainOrder']->UUpmode]);
		// .($row['mainOrder']->UUordermode!=0)?'(游客预订)':''
		//.($row['mainOrder']->UUstatus==0)?OrderRecord::$paystatusT[(string)$row['mainOrder']->UUpaystatus]:'')
		$objActSheet->setCellValue('N'.$n,OrderRecord::$_nSStatusT[(string)$row['mainOrder']->UUstatus]);
		$objActSheet->setCellValue('O'.$n,(string)$row['totalPrice']/100);
		$objActSheet->setCellValue('P'.$n,(string)$row['UUmemo']);
		//从订单
		if($row['childOrder']){
			$arr = array();
			$arr=$row['childOrder'];
			$arr = $arr[0];
			// print_r($arr);
			// exit;
			$n++;
			$objActSheet->setCellValue('A'.$n,$arr->UUordernum);    
			$objActSheet->setCellValue('B'.$n,$arr->UUordertime);
			$objActSheet->setCellValue('C'.$n,$arr->UUbegintime);
			$objActSheet->setCellValue('D'.$n,$arr->UUdtime!="0000-00-00 00:00:00"?(string)$arr->UUdtime:"未验证");
			if($_REQUEST['atype']=='buy'){
				$objActSheet->setCellValue('E'.$n,$anames[(string)$arr->UUaid]);
			}else{
				$objActSheet->setCellValue('E'.$n,$fnames[(int)$arr->fid]?$fnames[(int)$arr->fid]:(string)$arr->UUdname);
			}
			$objActSheet->setCellValue('F'.$n,(string)$arr->UUordername);
			$objActSheet->setCellValue('G'.$n,(string)$row['mainOrder']->UUordertel);
			$objActSheet->setCellValue('H'.$n,OrderRecord::$p_typeT[(string)$arr->UUp_type]);
			$objActSheet->setCellValue('I'.$n,$arr->UUordernum);
			$objActSheet->setCellValue('J'.$n,(string)$arr->UUltitle.(string)$arr->UUttitle);
			$objActSheet->setCellValue('K'.$n,(string)$arr->UUtprice/100);
			$objActSheet->setCellValue('L'.$n,(string)$arr->UUtnum);
			$objActSheet->setCellValue('M'.$n,OrderRecord::$paymodeT[(string)$arr->UUpmode]);
			// .($arr->UUordermode!=0)?'(游客预订)':''
			//.($arr->UUstatus==0)?OrderRecord::$paystatusT[(string)$arr->UUpaystatus]:'')
			$objActSheet->setCellValue('N'.$n,OrderRecord::$_nSStatusT[(string)$arr->UUstatus]);
			$objActSheet->setCellValue('O'.$n,'');
		}
	}
	
	
	$filename.=".xls";
	header("Content-Type: application/force-download");  
	header("Content-Type: application/octet-stream");  
	header("Content-Type: application/download");  
	header("Content-Disposition:inline;filename=$filename");  
	header("Content-Transfer-Encoding: binary");  
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
	header("Pragma: no-cache");  
	$objWriter->save('php://output');
	header('Location:http://www.baidu.com');
	exit;
	
}

?>